package com.guoning.office_system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guoning.office_system.model.AccountingRecord;
import com.guoning.office_system.model.User;
import com.guoning.office_system.service.AccountingRecordService;
import com.guoning.office_system.utils.Constants;
import com.guoning.office_system.utils.JsonResult;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/accountingRecord")
public class AccountingRecordController extends BaseController {
    @Autowired
    AccountingRecordService accountingRecordService;

    /**
     * 查询我申请的
     * @param request
     * @param pageIndex
     * @param pageSize
     * @param recordStatus
     * @return
     */
    @RequestMapping(value = "/select")
    public JsonResult select(HttpServletRequest request,
                             @RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "recordStatus", defaultValue = "") Integer recordStatus){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("userId", user.getId());
            criteria.addExtField("recordStatus", recordStatus);
            pageBean = accountingRecordService.selectByCondition(criteria, pageBean);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 查询我申请的
     * @param request
     * @param pageIndex
     * @param pageSize
     * @param recordStatus
     * @return
     */
    @RequestMapping(value = "/selectAll")
    public JsonResult selectAll(HttpServletRequest request,
                             @RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "recordStatus", defaultValue = "") Integer recordStatus){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("recordStatus", recordStatus);
            pageBean = accountingRecordService.selectByCondition(criteria, pageBean);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 发起申请
     * @param request
     * @param accountingRecord
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult insert(HttpServletRequest request, AccountingRecord accountingRecord){
        JsonResult jsonResult;
        try {
            if (accountingRecord.getBeginTime().getTime() >= accountingRecord.getEndTime().getTime()){
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "修改失败，开始时间不能大于结束时间");
                return jsonResult;
            }
            User user = getUserDate(request);
            accountingRecord.setUserId(user.getId());
            accountingRecord.setCreator(user.getUserName());
            List list = accountingRecordService.selectByTimeAndRecordId(accountingRecord);
            if(list.size() != 0){
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "申请失败，所选会议室在该时段已被占用");
                return jsonResult;
            }
            Long id = accountingRecordService.insert(accountingRecord);
            accountingRecord.setId(id);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "申请成功，请等待管理员审核", accountingRecord);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "申请异常");
        }
        return jsonResult;
    }

    /**
     * 修改会议申请
     * @param request
     * @param accountingRecord
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResult update(HttpServletRequest request, AccountingRecord accountingRecord){
        JsonResult jsonResult;
        try {
            if (accountingRecord.getBeginTime().getTime() >= accountingRecord.getEndTime().getTime()){
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "修改失败，开始时间不能大于结束时间");
                return jsonResult;
            }
            User user = getUserDate(request);
            accountingRecord.setModifier(user.getUserName());
            List list = accountingRecordService.selectByTimeAndRecordId(accountingRecord);
            if(list.size() != 0){
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "修改失败，所选会议室在该时段已被占用");
                return jsonResult;
            }
            accountingRecordService.updateByPrimaryKeySelective(accountingRecord);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "修改异常");
        }
        return jsonResult;
    }

    /**
     * 取消会议
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JsonResult delete(HttpServletRequest request, String params){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            List<AccountingRecord> list = JSONArray.parseArray(params, AccountingRecord.class);
            int i = accountingRecordService.updateForDelete(list, user);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return jsonResult;
    }

    /**
     * 审核
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public JsonResult check(HttpServletRequest request, String params){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            Map param = JSONObject.parseObject(params, Map.class);
            Integer status = JSONObject.parseObject(param.get("status").toString(), Integer.class);
            List<AccountingRecord> list = JSONArray.parseArray(param.get("list").toString(), AccountingRecord.class);
            int i = accountingRecordService.updateRecordStatus(list, user, status);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return jsonResult;
    }

}
